//
//  LoginViewController.m
//  Inspiration
//
//  Created by Mac on 15/10/15.
//  Copyright © 2015年 riverlj. All rights reserved.
//

#import "LoginViewController.h"
#import "UIView+xywh.h"
#import "UIColor+Mycolor.h"
#import "Masonry.h"
#import "AVOSCloudSNS.h"

@interface LoginViewController ()
{
    UITextField *_userNameTextField;
    UITextField *_passWordTextField;
    UIButton *_loginBtn;
    UIButton *_registerBtn;
}
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithR:47.0 G:53.0 B:68.0 A:1.0];
    
    //标题
    UILabel *logoLabel = [[UILabel alloc]init];
    logoLabel.font = [UIFont systemFontOfSize:36*sizeScale];
    logoLabel.text = @"欢迎来到励志语";
    logoLabel.textAlignment = NSTextAlignmentCenter;
    logoLabel.textColor = [UIColor colorWithR:86.0 G:182.0 B:89.0 A:1.0];
    [self.view addSubview:logoLabel];
    
    //副标题 101 104 117
    UILabel *subtitleLabel = [[UILabel alloc]init];
    subtitleLabel.font = [UIFont systemFontOfSize:12*sizeScale];
    subtitleLabel.text = @"天生丽质，我怎甘一生平庸";
    subtitleLabel.textColor = [UIColor colorWithR:101.0 G:104.0 B:107.0 A:1.0];
    subtitleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:subtitleLabel];
    
    //快速注册按钮
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerBtn setTitle:@"快速注册" forState:UIControlStateNormal];
    [registerBtn setTitle:@"快速注册" forState:UIControlStateHighlighted];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:16*sizeScale];
    [registerBtn setBackgroundImage:[UIColor imageWithColor:[UIColor colorWithR:86.0 G:182.0 B:89.0 A:1.0]] forState:UIControlStateNormal];
    [registerBtn setBackgroundImage:[UIColor imageWithColor:[UIColor colorWithR:41.0 G:95.0 B:48.0 A:1.0]] forState:UIControlStateHighlighted];
    [self.view addSubview:registerBtn];
    
    //登陆
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitle:@"登录" forState:UIControlStateHighlighted];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:16*sizeScale];
    [loginBtn setBackgroundImage:[UIColor imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
    [loginBtn setBackgroundImage:[UIColor imageWithColor:[UIColor colorWithR:41.0 G:95.0 B:48.0 A:1.0]] forState:UIControlStateHighlighted];
    [self.view addSubview:loginBtn];
    loginBtn.layer.backgroundColor = [[UIColor clearColor] CGColor];
    loginBtn.layer.borderColor = [[UIColor colorWithR:86.0 G:182.0 B:89.0 A:1.0] CGColor];
    loginBtn.layer.borderWidth = 1.0;
    [loginBtn.layer setMasksToBounds:YES];
    
    // 社交账号分割线
    UILabel *lineLabel = [[UILabel alloc]init];
    lineLabel.font = [UIFont systemFontOfSize:12 *sizeScale];
    lineLabel.text = @"——或者你可以使用以下方式登录——";
    lineLabel.textAlignment = NSTextAlignmentCenter;
    lineLabel.textColor = [UIColor colorWithR:101.0 G:104.0 B:107.0 A:1.0];
    [self.view addSubview:lineLabel];

    
    UIView *snsView = [[UIView alloc]init];
    snsView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:snsView];
    //社交账号
    for (int i=0; i<3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((SCREENWIDTH - 144*sizeScale)/4 * (i+1) + 48*sizeScale*i, 0, 48*sizeScale, 48*sizeScale);
        button.backgroundColor = [UIColor grayColor];
        button.layer.cornerRadius = 24*sizeScale;
        button.layer.masksToBounds = YES;
        if (i==0) {
            [button setBackgroundImage:[UIImage imageNamed:@"sns_qq"] forState:UIControlStateNormal];
        }
        if (i==1) {
            [button setBackgroundImage:[UIImage imageNamed:@"sns_wechat"] forState:UIControlStateNormal];
        }
        if (i==2) {
            [button setBackgroundImage:[UIImage imageNamed:@"sns_weibo"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(loginWithSina) forControlEvents:UIControlEventTouchUpInside];
        }
        
        [snsView addSubview:button];
        
    }

    //添加约束
    [logoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.greaterThanOrEqualTo(self.view).offset(100);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
    }];

    
    [subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(logoLabel.mas_bottom);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
    }];
    
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(40);
        make.right.equalTo(self.view.mas_right).offset(-40);
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY).offset(-50);
        make.height.equalTo(@40);
    }];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(registerBtn.mas_left);
        make.right.equalTo(registerBtn.mas_right);
        make.top.equalTo(registerBtn.mas_bottom).offset(20);
        make.height.equalTo(registerBtn.mas_height);
    }];
    
    [snsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-100);
        make.width.equalTo(self.view.mas_width);
        make.height.equalTo(@(48*sizeScale));
    }];
    
    [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(snsView.mas_top).offset(-20);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
    }];
}

- (void)loginWithSina{
    [AVOSCloudSNS setupPlatform:AVOSCloudSNSSinaWeibo withAppKey:@"3263376478" andAppSecret:@"22468a922a2173296135ea2e6b762212" andRedirectURI:@"http://www.baidu.com"];
    
    [AVOSCloudSNS loginWithCallback:^(id object, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        } else {
            NSString *accessToken = object[@"access_token"];
            NSString *username = object[@"username"];
            NSString *avatar = object[@"avatar"];
            NSDictionary *rawUser = object[@"raw-user"]; // 性别等第三方平台返回的用户信息
            //...
        }
    } toPlatform:AVOSCloudSNSSinaWeibo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
